package Day025_综合案例;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author zhangyan
 * @date 2020/5/20 10:13
 */
public class Client2 {
    public static void main(String[] args) {
        //创建Scanner对象
        Scanner sc = new Scanner(System.in);

        //一级菜单 循环显示
        while (true) {
            System.out.println("**********************");
            System.out.println("1.添加学生\t2.查询学生\t3.删除学生\t4.修改学生\t5.退出");
            System.out.println("**********************");
            System.out.println("请输入功能序号:");
            int i = sc.nextInt();
            //根据功能序号执行功能
            switch (i) {
                case 1:
                    //添加
                    addStudent(sc);
                    break;
                case 2:
                    //查询
                    selectStudent();
                    break;
                case 3:
                    //删除
                    deleteStudent(sc);
                    break;
                case 4:
                    //修改
                    setStudent(sc);
                    break;
                case 5:
                    //退出
                    System.out.println("欢迎再次使用!");
                    System.exit(0);
                    break;
            }
        }
    }

    //修改
    private static void setStudent(Scanner sc) {
        System.out.println("输入要修改学生的id:");
        int id = sc.nextInt();

        //按格式封装
        String message = "[5]" + id;

        //第一次连接
        //把message传给服务器
        try {
            //连接服务器
            Socket socket = new Socket("localhost", 8888);

            //获取输出流
            OutputStream os = socket.getOutputStream();

            //写出数据给服务器
            os.write(message.getBytes());

            //接收服务器返回的数据
            //通过Socket获取输入流
            InputStream is = socket.getInputStream();
            int i = is.read();
            //返回数据 判断
            if (i == 0) {
                //如果是0,就显示修改失败的信息,关闭连接,释放资源
                System.out.println("id不存在,修改失败");
                socket.close();
            } else {
                //如果是1,就显示可以修改的信息,关闭连接,释放资源
                System.out.println("可以修改");
                socket.close();
                //第二次连接,进行修改
                System.out.println("请输入要修改的学员姓名,0表示不修改:");
                String name = sc.next();
                System.out.println("请输入要修改的学员年龄,0表示不修改:");
                int age = sc.nextInt();
                System.out.println("请输入要修改的学员地址,0表示不修改:");
                String address = sc.next();

                //按格式封装数据,方便服务器解析
                String message2 = "[4]" + id + "," + name + "," + age + "," + address;
                //连接服务器
                Socket socket2 = new Socket("localhost", 8888);
                //获取输出流
                OutputStream os2 = socket2.getOutputStream();
                os2.write(message2.getBytes());

                //获取服务器返回数据
                InputStream is2 = socket2.getInputStream();
                byte[] bytes = new byte[1024];
                int len = is2.read(bytes);
                System.out.println(new String(bytes,0,len));
                //关流
                socket2.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除
    private static void deleteStudent(Scanner sc) {
        System.out.println("要删除的学生id:");
        int id = sc.nextInt();

        //按格式封装 传给服务端
        String message = "[3]" + id;

        try {
            //连接服务器
            Socket socket = new Socket("localhost", 8888);

            //获取输出流
            OutputStream os = socket.getOutputStream();

            //写出数据给服务器
            os.write(message.getBytes());

            //接收服务器返回的数据
            //通过Socket获取输入流
            //打印读取的数据
            InputStream is = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int i = is.read(bytes);
            System.out.println(new String(bytes, 0, i));
            //关闭资源
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //查询
    private static void selectStudent() {
        //按照指定格式传给服务器
        String message = "[2]";
        try {
            //连接服务器
            Socket socket = new Socket("localhost", 8888);

            //获取输出流
            OutputStream os = socket.getOutputStream();

            //写出数据给服务器
            os.write(message.getBytes());

            //接收服务器返回的数据
            //通过Socket获取输入流
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            //读取集合对象
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();

            //判断
            if (list.size() == 0) {
                System.out.println("没有数据");
            } else {
                //遍历打印
                for (Student stu : list) {
                    System.out.println(stu);
                }
            }
            //关闭资源
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //添加
    private static void addStudent(Scanner sc) {
        //输入要添加的学生信息
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生地址:");
        String address = sc.next();

        //按照一定格式拼接 方便服务器解析
        String message = "[1]" + name + "," + age + "," + address;

        try {
            //连接服务器
            Socket socket = new Socket("localhost", 8888);

            //获取输出流
            OutputStream os = socket.getOutputStream();

            //写出数据给服务器
            os.write(message.getBytes());

            //接收服务器返回的数据
            //通过Socket获取输入流
            //打印读取的数据
            InputStream is = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int i = is.read(bytes);
            System.out.println(new String(bytes, 0, i));
            //关闭资源
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
